﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// PaperFile 的摘要说明
/// </summary>
public class PaperFile
{

    public int StudentGuid;
    public int ExpertGuid;
    public int PaperFlag;
    public DateTime CreateTime;
    public string Title;
    public string FileName;
    public string Intro;
    public string Keywords;
    public int Guid;
    public int CategoryGuid;
    public PaperFile(int g)
    {
        this.Guid = g;
        this.StudentGuid = g;
        string sql = "select * from Paper where Guid=" + g.ToString();
        DataRow dr = Fun.CreateDr(sql);
        this.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
        this.PaperFlag = Convert.ToInt32(dr["Flag"]);
        this.Title = dr["Title"].ToString().Trim();
        this.FileName = dr["FileName"].ToString().Trim();
        this.Intro = dr["Intro"].ToString().Trim();
        this.Keywords = dr["Keywords"].ToString().Trim();
        this.StudentGuid = Convert.ToInt32(dr["StudentGuid"]);
        sql = "select ExpertGuid as n from Student where UserGuid=" + this.StudentGuid.ToString();
        this.ExpertGuid = Convert.ToInt32(Fun.GetField(sql));
        sql = "select CategoryGuid as n from Student where UserGuid=" + this.StudentGuid.ToString();
        this.CategoryGuid = Convert.ToInt32(Fun.GetField(sql));
    }

    public string CategoryName
    {
        get
        {
            return Category.CategoryName(this.CategoryGuid);
        }
    }

    public static void Delete(int g)
    {
        //string 
        string sql = "delete from PaperTip where PaperGuid=" + g.ToString();
        Fun.Execute(sql);
        sql = "select * from Paper where Guid=" + g.ToString();
        DataRow dr = Fun.CreateDr(sql);
        Fun.DeleteFile(Params.FilesFolderPath + dr["FileName"].ToString().Trim());
        int sg = Convert.ToInt32(dr["StudentGuid"]);
        sql = "delete from Paper where Guid=" + g.ToString();
        Fun.Execute(sql);
        sql = "select count(*) as n from Paper where StudentGuid="+sg.ToString();
        int c = Convert.ToInt32(Fun.GetField(sql));
        if (c < 1)
        {
            sql = "update Student set PaperFlag=0,ExpertGuid=0,PaperName='',CategoryGuid=0 where UserGuid="+sg.ToString();
            Fun.Execute(sql);
        }
    }

    public bool IsNewest
    {
        get
        {
            string sql = "select Top 1 Guid from Paper where StudentGuid=" + this.StudentGuid.ToString() + " order by Guid desc";
            DataTable dt = Fun.CreateDt(sql);
            if (dt.Rows.Count < 1)
            {
                return false;
            }
            int g = Convert.ToInt32(dt.Rows[0][0]);
            return g == this.Guid;
        }
    }
}
